<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @copyright (c) 2013
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @license Apache License 2.0
 * @version 1.0
 * @desc Search and retrieve data
 */
class Share extends CI_Controller {


	/**
	 * Defining controller properties
	 */
	public $viewData = array();
	private $language = "english";
	private $action;



	/**
	 *
	 * Loading common required models and langs
	 * Check session to prevent illegal access
	 */
	public function __construct(){
		parent::__construct();

		$this->load->helper("common_action");
		$this->action = new Action($this);

		$this->lang->load('common', $this->language);
		$this->lang->load('share', $this->language);

		$this->load->model("simple_data_model","simpleDataModel");
		$this->load->model("tag_model","tagModel");

		$this->simpleDataModel->init("USER","UID");
		$this->tagModel->init();
	}



	public function post(){

		if($this->action->userLogin()){
			
			$name			= $this->input->post("name");
			$image			= $this->input->post("image");
			$dishRate		= $this->input->post("dish_rate");
			$serviceRate	= $this->input->post("service_rate");
			$idRESTAURANT	= $this->input->post("restaurant_id");
			$idCUISINE		= $this->input->post("cuisine_id");

			$idUSER			= $this->input->post("user_id");
			$content		= $this->input->post("content");

			$this->action->addJSONItem("success", 0);
			$this->action->addJSONItem("error", 0);
			$this->action->addJSONItem("tag", $this->tagModel->getTag("post"));


			if($name && $image && $dishRate && $serviceRate && $idUSER && $content && $idRESTAURANT && $idCUISINE){
				
				$imageObject = imagecreatefromstring(base64_decode($image));
				if(!$imageObject){
					$this->action->addJSONItem("error", 1);
					$this->action->addJSONItem("message", $this->lang->line("error_image"));
				}else{
					
					imagejpeg($imageObject, "asset/image/".md5($image).".jpg");

					$this->load->model("specific_data_model","specificDataModel");
					$this->db->trans_start();
					$idDISH = $this->specificDataModel->setDish($name, md5($image).".jpg", $dishRate, $serviceRate, $idRESTAURANT, $idCUISINE);

					if($idDISH){
						$idPOST = $this->specificDataModel->setPost($idDISH, $idUSER, $name, $content);
							
						if($idPOST){
							$this->action->addJSONItem("success", 1);
							$this->action->addJSONItem("message", $this->lang->line("success"));
						}else{
							$this->action->addJSONItem("error", 1);
							$this->action->addJSONItem("message", $this->lang->line("error_insert"));
						}
					}else{
						$this->action->addJSONItem("error", 1);
						$this->action->addJSONItem("message", $this->lang->line("error_insert"));
					}

					$this->db->trans_complete();
				}
			}else{
				$this->action->addJSONItem("error", 1);
				$this->action->addJSONItem("message", $this->lang->line("error_inputs"));
			}

			$this->action->flushJSON();
		}
	}
}